package com.insleton.admin.config;

import jakarta.persistence.EntityManagerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.LocalEntityManagerFactoryBean;

import javax.inject.Inject;
import javax.sql.DataSource;
import java.util.Properties;

// Consider defining a bean named 'entityManagerFactory' in your configuration
// @Configuration
public class HibernateSessionConfiguration {

    @Inject
    private HibernateConfiguration hibernateConfiguration;


    @Inject
    @Bean("sessionFactory")
    public LocalSessionFactoryBean getSessionFactory(DataSource dataSource) {
        return buildLocalSessionFactory(dataSource);

    }

    /**
     * 设置Hibernate的配置属性
     *
     * @return Properties
     */
    private Properties getHibernateProperties(){
        Properties hibernateProperties = new Properties();
        hibernateProperties.put("hibernate.dialect", hibernateConfiguration.getDialect());
        hibernateProperties.put("default_schema", hibernateConfiguration.getDefaultSchema());
        hibernateProperties.put("hibernate.show_sql", hibernateConfiguration.getShowSql());
        hibernateProperties.put("hibernate.format_sql", hibernateConfiguration.getFormatSql());
        hibernateProperties.put("hibernate.hbm2ddl.auto", hibernateConfiguration.getHbm2ddlAuto());
        hibernateProperties.put("hibernate.temp.use_jdbc_metadata_defaults", hibernateConfiguration.getUseJdbcMetadataDefaults());
        return hibernateProperties;

    }

    /**
     * 构建LocalSessionFactoryBean实例
     *
     * @param dataSource 构建实例所使用的的数据源
     * @return LocalSessionFactoryBean
     */
    private LocalSessionFactoryBean buildLocalSessionFactory(DataSource dataSource){
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setPackagesToScan("com.insleton.*", "com.vteba.*");
        sessionFactory.setHibernateProperties(getHibernateProperties());
        return sessionFactory;
    }

    @Bean
    @jakarta.inject.Inject
    public EntityManagerFactory containerEntityManagerFactory(DataSource dataSource) {
        // 这个多了容器感知
        LocalContainerEntityManagerFactoryBean containerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        // LocalEntityManagerFactoryBean factoryBean = new LocalEntityManagerFactoryBean();
        containerFactoryBean.setDataSource(dataSource);
        containerFactoryBean.setPackagesToScan("com.insleton.*", "com.vteba.*");
        return containerFactoryBean.getNativeEntityManagerFactory();
    }

    @Bean
    @jakarta.inject.Inject
    public EntityManagerFactory entityManagerFactory(DataSource dataSource) {
        // 这个多了容器感知
        // LocalContainerEntityManagerFactoryBean containerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        // containerFactoryBean.setDataSource(dataSource);
        // containerFactoryBean.setPackagesToScan("com.insleton.*", "com.vteba.*");
        // return containerFactoryBean.getNativeEntityManagerFactory();
        LocalEntityManagerFactoryBean factoryBean = new LocalEntityManagerFactoryBean();

        factoryBean.createNativeEntityManager(null);
        return factoryBean.getNativeEntityManagerFactory();
    }
}


